/**
 * 
 */
package org.starsource.forum.server.domain;

import java.io.Serializable;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.starsource.forum.client.domain.MessageObject;
import org.starsource.forum.client.domain.SessionObject;
import org.starsource.forum.client.util.Assertion;


/**
 * @author james
 *
 */
public class Message implements HasId, Serializable {

	private static final long serialVersionUID = 3245026930384911893L;
	
	private int id;
	
	private ForeignKey<Session> session = new ForeignKey<Session>();
	
	private ForeignKey<User> owner = new ForeignKey<User>();
	
	private String content;
	
	private Timestamp createTimestamp;
	
	private Timestamp updateTimestamp;
	
	private Set<Resource> resources = new HashSet<Resource>();
	
	public Message(User owner, String content, Session session, Timestamp createTimestamp, Timestamp updateTimestamp) {
		this.owner.setObject(owner);
		this.session.setObject(session);
		this.content = content;
		this.createTimestamp = createTimestamp;
		this.updateTimestamp = updateTimestamp;
	}
	
	public Message(int id, User owner, String content, Session session, Timestamp createTimestamp, Timestamp updateTimestamp) {
		this.id = id;
		this.owner.setObject(owner);
		this.session.setObject(session);
		this.content = content;
		this.createTimestamp = createTimestamp;
		this.updateTimestamp = updateTimestamp;
	}

	public Message() {
	}

	public int getId() {
		return this.id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public Session getSession() {
		return this.session.getObject();
	}
	
	public void setSession(Session session) {
		this.session.setObject(session);
	}
	
	public int getSessionId() {
		return this.session.getId();
	}
	
	public void setSessionId(int sessionId) {
		this.session.setId(sessionId);
	}
	
	public User getOwner() {
		return this.owner.getObject();
	}
	
	public void setOwner(User owner) {
		this.owner.setObject(owner);
	}
	
	public int getOwnerId() {
		return this.owner.getId();
	}
	
	public void setOwnerId(int ownerId) {
		this.owner.setId(ownerId);
	}

	public String getContent() {
		return this.content;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	
	public Timestamp getCreateTimestamp() {
		return this.createTimestamp;
	}
	
	public void setCreateTimestamp(Timestamp createTimestamp) {
		this.createTimestamp = createTimestamp;
	}
	
	public Timestamp getUpdateTimestamp() {
		return this.updateTimestamp;
	}
	
	public void setUpdateTimestamp(Timestamp updateTimestamp) {
		this.updateTimestamp = updateTimestamp;
	}
	
	public Date getCreateDate() {
		return new Date(this.createTimestamp.getTime());
	}
	
	public Time getCreateTime() {
		return new Time(this.createTimestamp.getTime());
	}
	
	public Set<Resource> getResources() {
		return this.resources;
	}
	
	public void setResources(Set<Resource> resources) {
		this.resources = resources;
	}
	
	@Override
	public boolean equals(Object obj) {
		try {
			Message object = (Message)obj;
			
			if (this.id != object.getId()) return false;
			if (this.session.getId() != object.session.getId()) return false;
			if (this.owner.getId() != object.owner.getId()) return false;
			if (!Assertion.isEqual(this.content, object.content)) return false;
			if (!Assertion.isEqual(this.createTimestamp, object.createTimestamp)) return false;
			if (!Assertion.isEqual(this.updateTimestamp, object.updateTimestamp)) return false;

			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public MessageObject getMessageObject(SessionObject sessionObject) {
		MessageObject message = new MessageObject(this.id, null, this.content, sessionObject, this.createTimestamp, this.createTimestamp);
		if (this.owner.getObject() != null) {
			message.setOwner(this.owner.getObject().getUserObject());
		}
		
		
		Iterator<Resource> resources = this.resources.iterator();
		while (resources.hasNext()) {
			message.getResources().add(resources.next().getResourceObject(message));
		}
		return message;
	}

}
